package com.winchain.chainbill.bill.biz.repurchase.cmd;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.service.AcctTxRecService;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.cmd.TxCmd;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRpurTranCmd extends TxCmd<CbRpur> {
	@Autowired
	private AcctTxRecService AcctTxRecService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;
	@Autowired
	private CbDctService cbDctService;

	public boolean execute(CommandContext<Bill<CbRpur>> context) throws Exception {
		boolean timeout = ((Boolean) context.getValue("Timeout")).booleanValue();
		if (!timeout) {
			AcctTxRec tran = getAcctTxRec(context);

			this.AcctTxRecService.save(tran);
			context.setValue("acctTxRec", tran);
		}
		return false;
	}

	private AcctTxRec getAcctTxRec(CommandContext<Bill<CbRpur>> context) {
		CbRec record = ((Bill) context.getSource()).getRec();

		CbRpur cbRpur = (CbRpur) ((Bill) context.getSource()).getSource();
		String billId = record.getBillId();
		String buyerId = cbRpur.getBuyerId();

		CbDct dct = (CbDct) this.cbDctService.selectByKey(cbRpur.getDctId());
		String finId = dct.getFinId();

		BigDecimal billAmt = cbRpur.getRpurAmt();
		String optType = "qt_trd";

		Object bizType = context.getValue("bizType");
		if (bizType == null) {
			throw new AcctException("������������������");
		}
		String abs = "����������";
		String creditAcctUse = "1";
		String acctUse = "1";

		AcctTxRec tranRcd = new AcctTxRec();
		tranRcd.setTxId(SystemUtil.getUUID());
		tranRcd.setTxSn(tranRcd.getTxId());

		AcctBas creditAcct = getAcctBas(finId, creditAcctUse, null);

		String tranAcctNo = creditAcct.getAcctNo();
		tranRcd.setTxAcctNo(tranAcctNo);
		tranRcd.setTxAcctNm(creditAcct.getAcctNm());
		tranRcd.setTxBankNm(creditAcct.getFiNm());
		tranRcd.setTxAmt(billAmt);
		tranRcd.setTxDt(SysDateUtil.getSystemDate());
		tranRcd.setTxTs(SysDateUtil.getSystemDateTime());
		tranRcd.setTxAbs(abs);
		tranRcd.setTxType((String) bizType);
		tranRcd.setOptType(optType);

		tranRcd.setTxSts("I");

		AcctBas acct = getAcctBas(buyerId, acctUse, null);
		tranRcd.setCpAcctNo(acct.getAcctNo());
		tranRcd.setCpAcctNm(acct.getAcctNm());
		tranRcd.setCpBankNm(acct.getFiNm());
		tranRcd.setCurrCode(AssetCode.XCB.name());
		tranRcd.setTxChnCode("C");
		tranRcd.setTxCashFlag("N");
		tranRcd.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		tranRcd.setSrcCode(sourceCode);
		tranRcd.setBizId(billId);
		tranRcd.setDataScLvl("B");

		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(tranRcd.getTxAcctNo(), tranRcd.getCurrCode(), null,
				null);
		tranRcd.setTxBkpId(txAcctBkp.getBkpId());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(tranRcd.getCpAcctNo(), tranRcd.getCurrCode(), null,
				null);
		tranRcd.setCpBkpId(cpAcctBkp.getBkpId());

		return tranRcd;
	}
}
